package org.softmed.rest.editor;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.softmed.rest.admin.interfaces.CloseListener;
import org.softmed.swing.IconManager;

public class ModuleManagerGUI extends JPanel implements ActionListener {

//	JButton create = new JButton("Create Module", IconManager.newDocument); // OK


//	ButtonGroup group = new ButtonGroup();
	JButton configuration = new JButton("New Configuration");
	JButton module = new JButton("New Module");

	JButton changeMode = new JButton("Close");
	JLabel mode = new JLabel("Select:");

	int width = 150;
	int height = 20;
	private RESTEditor editor;

	public ModuleManagerGUI(RESTEditor editor) {
		this.editor = editor;
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

//		group.add(configuration);
//		group.add(module);

		// configuration.addVetoableChangeListener(this);

		setMinimumSize(new Dimension(150, 30));
//		mode.setVisible(false);
		add(mode);

		addButton(configuration);
		addButton(module);

		configuration.setSelected(true);
		configuration.setEnabled(true);
		module.setEnabled(true);
		addButton(changeMode);
		resizeButton(changeMode, 150, height);

		add(new JLabel("Module Manager"));
//		addButton(create);


	}

	protected void addButton(AbstractButton button) {
		resizeButton(button, width, height);
		button.addActionListener(this);
		button.setEnabled(false);
		add(button);
	}

	protected void resizeButton(AbstractButton button, int width, int height) {
		button.setPreferredSize(new Dimension(width, height));
		button.setMaximumSize(new Dimension(width, height));
		button.setMinimumSize(new Dimension(width, height));
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();
		configuration.setEnabled(false);
		module.setEnabled(false);

		if (source == configuration) {
			editor.setConfigurationMode();
			setConfigurationMode();

		} else if (source == module) {
			editor.setModuleMode();
			setModuleMode();

		} else if (source == changeMode) {
			editor.close();
			configuration.setEnabled(true);
			module.setEnabled(true);
			changeMode.setEnabled(false);
		}  

	}

	public void close() {
//		create.setEnabled(false);
//		mode.setVisible(false);
		mode.setText("Select:");
//		add.setEnabled(false);
//		remove.setEnabled(false);
		configuration.setEnabled(true);
		module.setEnabled(true);
	}

	public void setConfigurationMode() {
//		mode.setVisible(true);
		mode.setText("Editing Configuration");
		configuration.setEnabled(false);
		module.setEnabled(false);

//		create.setEnabled(false);
//		add.setEnabled(true);
//		remove.setEnabled(true);
		changeMode.setEnabled(true);

	}

	public void setModuleMode() {
//		mode.setVisible(true);
		mode.setText("Editing Module");
		configuration.setEnabled(false);
		module.setEnabled(false);

//		create.setEnabled(true);
//		add.setEnabled(false);
//		remove.setEnabled(false);
		changeMode.setEnabled(true);

	}

}
